<template>
  <div>
    <Modal
      :value="modalState"
      :title="title"
      :loading="loading"
      ok-text="保存"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="35%">
      <Form
        ref="contentSettingModal"
        :model="row"
        :rules="ruleInline"
        style="width: 100%"
        :label-width="100">
        <FormItem prop="name" label="名称：" style="width: 250px">
          <Input type="text" v-model="row.name" placeholder="请输入任务名称" />
        </FormItem>
        <FormItem prop="btime" style="width: 250px" label="开始时间：">
          <DatePicker v-model="row.btime" type="date" placeholder="请选择开始时间"></DatePicker>
        </FormItem>
        <FormItem prop="etime" style="width: 250px" label="结束时间：">
          <DatePicker v-model="row.etime" type="date" placeholder="请选择结束时间"></DatePicker>
        </FormItem>
        <FormItem prop="content" style="width: 250px" label="简单描述：">
          <Input type="text" v-model="row.content" placeholder="请输入简单描述" />
        </FormItem>
        <FormItem prop="img" style="width: 250px" label="上传图片：">
          <upload-img @upload-success="handleUpload"></upload-img>
        </FormItem>
        <FormItem prop="link" style="width: 250px" label="跳转链接：">
          <Input type="text" v-model="row.link" placeholder="请输入跳转链接" />
        </FormItem>
        <FormItem prop="openState" label="是否启动：">
          <i-switch v-model="row.openState" />
        </FormItem>
      </Form>
      <div style="border: 1px solid #000; width: 285px; height: 570px; margin-left: 320px; margin-top: -430px"></div>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import UploadImg from '_c/upload-img'
export default {
  name: 'ContentSettingModal',
  components: {
    UploadImg
  },
  data () {
    return {
      title: '',
      loading: true,
      imgUrl: '',
      ruleInline: {}
    }
  },
  updated () {
    if (this.row.id && this.row.id !== '') {
      this.title = '编辑弹窗页面'
    } else {
      this.title = '添加弹窗页面'
    }
  },
  computed: {
    ...mapState({
      contentModalList: state => state.contentModal.taskList,
      row: state => state.contentModal.row,
      modalState: state => state.modal.modalState
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.contentSettingModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('edit-content-setting-valid', {
          id: this.row.id ? this.row.id : '',
          title: this.row.title ? this.row.title : '',
          startTime: this.row.startTime ? this.$moment(this.row.startTime).format('YYYY-MM-DD') : '',
          endTime: this.row.endTime ? this.$moment(this.row.endTime).format('YYYY-MM-DD') : '',
          description: this.row.description,
          img: this.imgUrl,
          link: this.row.link,
          openState: this.row.openState,
          state: this.row.openState === true ? 1 : 0
        })
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['contentSettingModal'].resetFields()
    },
    handleUpload (url) {
      this.imgUrl = url
    }
  }
}
</script>
